#include <iostream>
#include "myTask.hpp"
#include"myBlockQueue.hpp"

void *_customer(void *args)
{
    //sleep(1);

    blockQueue<Task>* q=static_cast<blockQueue<Task>*>(args);
    while(1)
    {
        Task out;
        q->pop(out);
        out();

        //out.taskCustomerPrintf();
        cout << pthread_self() << " | consumer data: " << out.formatArg() << out.formatRes() << std::endl;
    }
    // sleep(3);
    // stack<int> *_q = static_cast<stack<int> *>(args);
    // while (_cap == 0)
    // {
    //     pthread_mutex_lock(&mutex);
    //     pthread_cond_wait(&cus, &mutex);
    //     pthread_mutex_unlock(&mutex);
    // }
    // while (1)
    // {
    //     pthread_mutex_lock(&mutex);
    //     int ret = _q->top();
    //     _q->pop();
    //     _cap--;
    //     cout << ret << endl;
    //     pthread_cond_signal(&prod);
    //     pthread_mutex_unlock(&mutex);
    // }
}
void *_producer(void *args)
{
    string _op="+-*/%";
    blockQueue<Task>* q=static_cast<blockQueue<Task>*>(args);
    while(1)
    {
        int x=rand()%100;
        int y=rand()%10;
        char op=_op[rand()%5];
        Task task(x,y,op);
        int in=x*y;
        q->push(task);
        cout << pthread_self() << " | productor Task: " <<  task.formatArg() << "?" << std::endl;


    }
    // sleep(3);
    // stack<int> *_q = static_cast<stack<int> *>(args);
    // int x = rand() % 100;
    // int y = rand() % 100;
    // int z = 0;
    // if (y)
    // {
    //     z = x / y;
    // }
    // cout<<"x= "<<x<<"  y= "<<y<<endl;
    // while (g_cap == _cap)
    // {
    //     pthread_mutex_lock(&mutex);
    //     pthread_cond_wait(&cus, &mutex);
    //     pthread_mutex_unlock(&mutex);
    // }
    // while (1)
    // {
    //     pthread_mutex_lock(&mutex);
    //     _q->push(z);
    //     _cap++;
    //     pthread_cond_signal(&cus);
    //     pthread_mutex_unlock(&mutex);
    // }
}
int main()
{
    Task task;
    srand((size_t)(time(nullptr)));
    blockQueue<Task>* q=new(blockQueue<Task>)(5);
    pthread_t customer[3],producer[5]; 
    for(int i=0;i<sizeof(customer)/sizeof(customer[0]);i++)
    {
        // char* name=new char(64);
        // snprintf(name,64,"customerThread-- %d :",i+1);
        // q->Name(name);
        pthread_create(customer+i,nullptr,_customer,q);

    }
    for(int i=0;i<sizeof(producer)/sizeof(producer[0]);i++)
    {
        //char* name=new char(64);
        //snprintf(name,64,"producerThread-- %d :",i+1);
        pthread_create(producer+i,nullptr,_producer,q);

    }
    for(int i=0;i<sizeof(producer)/sizeof(producer[0]);i++)
    {
        pthread_join(customer[i],nullptr);
    }
    for (int i = 0; i < sizeof(customer) / sizeof(customer[0]); i++)
    {
        pthread_join(producer[i], nullptr);
    }


    // stack<int> _queue; // 存放统一资源
    // srand((int)time(nullptr));
    // pthread_t customer;
    // pthread_t producer;
    // // 需要看见同一资源，用队列
    // pthread_create(&customer, nullptr, _customer, &_queue); // 创建线程
    // pthread_create(&producer, nullptr, _producer, &_queue);
    // pthread_join(customer, nullptr); // 回收线程
    // pthread_join(producer, nullptr);
    // return 0;
}